<template>
	<view class="editPassWord">
		<uni-forms ref="form" :modelValue="formData" :rules="rules" :label-width="80">
			<uni-forms-item label="新密码" required name="new_passwd1">
				<uni-easyinput type="text" v-model="formData.new_passwd1" placeholder="请输入新密码" />
			</uni-forms-item>
			<uni-forms-item label="确认密码" required name="new_passwd2">
				<uni-easyinput type="text" v-model="formData.new_passwd2" placeholder="确认密码" />
			</uni-forms-item>
		</uni-forms>
		<u-button type="primary" iconColor="#0062ad" @click="submit">修改</u-button>
	</view>
</template>

<script>
	import {editPasswd} from "../../api/login.js"
	export default {
		data() {
			return {
				// 表单数据
				formData: {
					old_passwd: uni.getStorageSync("passwd"),
					new_passwd1: '',
					new_passwd2: ''
				},
				rules: {
					// 对name字段进行必填验证
					old_passwd: {
						rules: [{
							required: true,
							errorMessage: '请输入原密码',
						}]
					},
					// 对name字段进行必填验证
					new_passwd1: {
						rules: [{
							required: true,
							errorMessage: '请输入新密码',
						}]
					},
					// 对name字段进行必填验证
					new_passwd2: {
						rules: [{
							required: true,
							errorMessage: '请输入确认密码',
						}, {
							validateFunction: function(rule, value, data, callback) {
								if (value != data.new_passwd1) {
									callback('与新密码不一致')
								}
								return true
							}
						}]
					}
				}
			}
		},
		methods: {
			submit() {
				this.$refs.form.validate().then(res => {
					let param ={
						old_passwd:this.formData.old_passwd,
						new_passwd:this.formData.new_passwd1
					}
					editPasswd(param).then(res =>{
						console.log(res)
						if(res.data.code == '1'){
							uni.clearStorage("guid");
							uni.reLaunch({
								url: '/pages/login/index'
							});
						}
					})
				}).catch(err => {
					console.log('表单错误信息：', err);
				})
			}
		}
	}
</script>
<style lang="scss">
	.editPassWord {
		padding: 60rpx 40rpx 0;

		.uni-forms {
			margin-bottom: 100rpx;
		}

		.u-button {
			background: #0062AD;
			border-color: #0062AD;
		}
	}
</style>
